﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using COMOSOFT.Class;
using COMOSOFT.MatarialManagement.Forms;
using System.Data.SqlClient;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.BandedGrid;
using DevExpress.XtraEditors;


namespace COMOSOFT.MatarialManagement.Forms
{
    public partial class FrmStyleDetails : Form
    {
        int MALID;       
        bool result = false;
        DataTable dtbMADetails;
        ClassShare cls = new ClassShare("FrmStyleDetails");
        Int32 TotalQty;
        
       struct colHeader
       {
           public string PO;
           public string color;
           public int qty;
       }

       List<colHeader> Listheader = new List<colHeader>();

        public FrmStyleDetails(int MALID) 
        {            
            InitializeComponent();
            this.MALID = MALID;
            if (MALID == -1)
            {
                mAListMasterTableAdapter.Fill(dataSetMatarial.MAListMaster, MALID);
            }                  
        }

        #region[Refresh Data]
        private void RefreshMALDetails()
        {
            this.sp_MAListDetailViewTableAdapter.Fill(dataSetMatarial.sp_MAListDetailView, MALID);
        }
        #endregion

        #region[AddNew]       

        public void AddNew()
        {
            mAListMasterBindingSource.AddNew();
            sp_CustomerListTableAdapter.Fill(dataSetOrderManagement.sp_CustomerList, ClassShare.Usid);
            panel_Material.Enabled = false;
            txt_issuedate.Text = DateTime.Now.ToShortDateString();
            lookUp_EID.EditValue = Convert.ToInt64(ClassShare.Usid);
        }
        #endregion

        #region [LoadData]
        public void LoadData()
        {
            try
            {
                sp_CustomerListTableAdapter.Fill(dataSetOrderManagement.sp_CustomerList, ClassShare.Usid);
                sp_SelectAMListDetailsTableAdapter.Fill(dataSetMatarial.sp_SelectAMListDetails);
                if (MALID > 0)
                {
                    mAListMasterTableAdapter.Fill(dataSetMatarial.MAListMaster, MALID);
                    sp_MAListDetailViewTableAdapter.Fill(dataSetMatarial.sp_MAListDetailView, MALID);
                    this.Text = "MAPO: " + txt_MalNo.Text;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region [Update MAMaster]
        public void UpdateData()
        {
            try
            {
                if (MALID == -1)
                {
                    mAListMasterBindingSource.EndEdit();
                    mAListMasterTableAdapter.Update(dataSetMatarial.MAListMaster);
                    dataSetMatarial.MAListMaster.AcceptChanges();
                    if (panel_Material.Enabled == false)
                    {
                        panel_Material.Enabled = true;
                        MALID = Convert.ToInt32(txt_malid.Text);
                    }
                }
                else
                {
                    mAListMasterBindingSource.EndEdit();
                    mAListMasterTableAdapter.Update(dataSetMatarial.MAListMaster);
                    dataSetMatarial.MAListMaster.AcceptChanges();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region [Load MaterialDetails]
        private void FrmStyleDetails_Load(object sender, EventArgs e)
        {               
            this.employeeTableAdapter.Fill(this.dataSetOrderManagement.Employee);            
            this.orderDetailsTableAdapter.Fill(this.dataSetOrderManagement.OrderDetails);
            this.sp_SelectAMListDetailsTableAdapter.Fill(this.dataSetMatarial.sp_SelectAMListDetails);          
        }
        #endregion

        #region [Update MA Details]
        private void gridView_MatarialDetails_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            try
            {
                if (result == true)
                {
                    spMAListDetailViewBindingSource.EndEdit();
                    sp_MAListDetailViewTableAdapter.Update(dataSetMatarial.sp_MAListDetailView);
                    dataSetMatarial.sp_MAListDetailView.AcceptChanges();
                    MessageBox.Show("Update successfully!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region [Choose MA Details]
        private void repositorySelectMADetails_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            var glk = bandedGridView_Material.ActiveEditor as GridLookUpEdit;
            var view = glk.Properties.View as GridView;
            try
            {         
                 int MAID = Convert.ToInt32(view.GetFocusedRowCellValue("MAID"));                
                 PopUpMA PUMA = new PopUpMA(MALID, MAID);
                 PUMA.ShowDialog();
               
                sp_ChooseMASourceTableAdapter.Fill(dataSetMatarial.sp_ChooseMASource, MALID);
                sp_ChooseColorTableAdapter.Fill(dataSetMatarial.sp_ChooseColor, MAID);           
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                view.FocusedColumn = view.Columns[0];
            }
        }
        #endregion

        #region [Refresh - Delete MADetails]
        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RefreshMALDetails();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Delete row selected?", "Deletion....?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    int MALDID = Convert.ToInt32(bandedGridView_Material.GetFocusedDataRow()["MALDID"]);
                    if (MALDID != 0)
                    {
                        cls.UpdateData("sp_MAListDetailDelete", new string[1] { "@MALDID" }, new object[1] { MALDID });
                        RefreshMALDetails();
                    }
                }
            }
            catch
            {
                MessageBox.Show("No item for delete!","Can't not Delete!",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
        }
        #endregion

        #region [Delete MALMaster]
        public void DeleteMaster()
        {
            try
            {
                if (MessageBox.Show("Are you sure you want to delete?", "Delete item", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    this.dataSetMatarial.MAListMaster.Rows[0].Delete();
                    mAListMasterTableAdapter.Update(dataSetMatarial.MAListMaster);
                    dataSetMatarial.MAListMaster.AcceptChanges();
                    this.Close();
                }
            }
            catch
            {
                MessageBox.Show("You can't delete this item, Please check objects relation!", "Error Delete",MessageBoxButtons.OK, MessageBoxIcon.Error);
                mAListMasterTableAdapter.Fill(dataSetMatarial.MAListMaster, MALID);                
            }
        }
        #endregion

        #region [Load CusStyleNo]
        private void txt_customer_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                sp_CusStyleNoTableAdapter.Fill(dataSetMatarial.sp_CusStyleNo, txt_customer.EditValue.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region [MAStyleNo]
        private void gridLookUpEdit_styleno_EditValueChanged(object sender, EventArgs e)
        {
            if (txt_customer.EditValue != null)
            {
                string Cus = txt_customer.EditValue.ToString();
                string StyleNo = gridLookUpEdit_styleno.EditValue.ToString();
                if (Cus != null && StyleNo != null)
                {
                    sp_CusPOTableAdapter.Fill(dataSetMatarial.sp_CusPO, Cus, StyleNo);
                }
                txt_MalNo.Text = gridLookUpEdit_styleno.EditValue + " - " + txt_PO.EditValue;
            }
        }

        private void txt_PO_EditValueChanged(object sender, EventArgs e)
        {
            txt_MalNo.Text = gridLookUpEdit_styleno.EditValue + " - " + txt_PO.EditValue;
        }
        #endregion     

        #region [Init BackgroundWorker]
        public void RunBackroundWorker()
        {
            backgroundWorker_MADetails.RunWorkerAsync();
        }
       #endregion

        private void LoadDataMA()
        {
            DataTable dtb = cls.GetData("sp_MAListDetailView", new string[1] { "@malid" }, new object[1] { MALID });            
            //bool checkcons = false;
            Listheader.Clear();
            foreach (DataRow dtr in dtb.Rows)
            {
                colHeader nodeheader = new colHeader();
                nodeheader.PO=dtr["PO"].ToString();
                nodeheader.color = dtr["color"].ToString();
                nodeheader.qty =Convert.ToInt32( dtr["Quantity"]);
                Listheader.Add(nodeheader);
            }
            dtbMADetails = new DataTable();
            dtbMADetails.Columns.Add("MALID");
            dtbMADetails.Columns.Add("MALDID");
            dtbMADetails.Columns.Add("OID");
            dtbMADetails.Columns.Add("MAID");
            dtbMADetails.Columns.Add("MADID");
            dtbMADetails.Columns.Add("Art");
            dtbMADetails.Columns.Add("SubArt");
            dtbMADetails.Columns.Add("EnName");
            dtbMADetails.Columns.Add("Content");
            dtbMADetails.Columns.Add("Suplier");
            dtbMADetails.Columns.Add("Width");
            dtbMADetails.Columns.Add("Units");
            dtbMADetails.Columns.Add("UnitPrice");
            foreach (colHeader node in Listheader)
            {
                dtbMADetails.Columns.Add("PO:"+node.PO);
                dtbMADetails.Columns.Add("Qty-PO:" + node.PO);
            }
            dtbMADetails.Columns.Add("TotalQty");
            dtbMADetails.Columns.Add("No");
            dtbMADetails.Columns.Add("Quantity");
            dtbMADetails.Columns.Add("EstCons", System.Type.GetType("System.Decimal"));
            dtbMADetails.Columns.Add("CusCons", System.Type.GetType("System.Decimal"));
            dtbMADetails.Columns.Add("ActCons", System.Type.GetType("System.Decimal"));
            dtbMADetails.Columns.Add("BookCons", System.Type.GetType("System.Decimal"));
            dtbMADetails.Columns.Add("Wastage", System.Type.GetType("System.Decimal"));
            dtbMADetails.Columns.Add("Type");
            dtbMADetails.Columns.Add("Using");
            dtbMADetails.Columns.Add("Remarks");

            foreach (DataRow row in dtb.Rows)
            {
                DataRow newrow = dtbMADetails.NewRow();
                newrow["MALDID"] = row["MALDID"];
                newrow["Art"] = row["Art"];
                newrow["SubArt"] = row["SubArt"];
                newrow["EnName"] = row["EnName"];
                newrow["Content"] = row["Content"];
                newrow["Suplier"] = row["MASUPID"];
                newrow["Width"] = row["Width"];
                newrow["Units"] = row["Units"];
                newrow["UnitPrice"] = row["UnitPrice"];
                foreach (colHeader node in Listheader)
                {
                    newrow["PO:"+node.PO] = node.color;
                    newrow["Qty-PO:" + node.PO] = node.qty;
                    TotalQty += node.qty;
                }
                newrow["TotalQty"] = TotalQty;
                newrow["No"] = row["No"];
                newrow["Quantity"] = row["Quantity"];
                newrow["EstCons"] = row["EstCons"];
                newrow["CusCons"] = row["CusCons"];
                newrow["ActCons"] = row["ActCons"];
                newrow["BookCons"] = row["BookCons"];
                newrow["Wastage"] = row["Wastage"];
                newrow["Type"] = row["Type"];
                newrow["Using"] = row["Using"];
                newrow["Remarks"] = row["Remarks"];
                dtbMADetails.Rows.Add(newrow);
            }           
        }

        private void backgroundWorker_MADetails_DoWork(object sender, DoWorkEventArgs e)
        {
            LoadDataMA(); 
        }

        private void backgroundWorker_MADetails_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {            
            #region CreateView
            gridControl_Material.DataSource = null;
            BandedGridView view = gridControl_Material.MainView as BandedGridView;
            view.Bands.Clear();
            view.Columns.Clear();
            view.GroupSummary.Clear();
            GridBand bandItem = view.Bands.AddBand("Item");
            GridBand bandColor = view.Bands.AddBand("Color");
            GridBand bandItemDetails = view.Bands.AddBand("Item Details");
            GridBand bandQty = view.Bands.AddBand("Quantity");
            GridBand bandCons = view.Bands.AddBand("Unit/Cons/Wastage");
            GridBand bandNeed = view.Bands.AddBand("Need Qty");
            GridBand bandPrice = view.Bands.AddBand("Price");
            GridBand bandRemark = view.Bands.AddBand("Using/Remark");
            view.ColumnPanelRowHeight = 70;

            foreach (GridBand band in view.Bands)
            {
                band.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                band.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Top;
                band.AppearanceHeader.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
                band.AppearanceHeader.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold);
                band.AppearanceHeader.ForeColor = System.Drawing.Color.Blue;
                band.AppearanceHeader.Options.UseFont = true;
                band.AppearanceHeader.Options.UseForeColor = true;
            }
            System.Threading.Thread.Sleep(10);
            progressBarReport.EditValue = 85;
            //*******************************************************************************
            BandedGridColumn colMAID = view.Columns.AddField("MALDID");
            BandedGridColumn colNo = view.Columns.AddVisible("No") as BandedGridColumn;           

            BandedGridColumn colUsing = view.Columns.AddVisible("Using") as BandedGridColumn;
            colUsing.Caption = "Using/Position";
            colUsing.ColumnEdit = persistentRepository1.Items["repositoryItemMemoExEditUsing"];
            colUsing.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;


            BandedGridColumn colArt = view.Columns.AddVisible("Art") as BandedGridColumn;
            BandedGridColumn colSubArt = view.Columns.AddVisible("SubArt") as BandedGridColumn;
            BandedGridColumn colItem = view.Columns.AddVisible("EnName","Item") as BandedGridColumn;
            BandedGridColumn colBySize = view.Columns.AddField("BySize") as BandedGridColumn;

            colNo.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
            colMAID.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;           
            colNo.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;           
            colArt.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            colSubArt.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            colItem.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

            colMAID.OwnerBand = bandItem;            
            colNo.OwnerBand = bandItem;           
            colUsing.OwnerBand = bandItem;
            colArt.OwnerBand = bandItem;
            colArt.OptionsColumn.ReadOnly = true;
            colSubArt.OwnerBand = bandItem;
            colSubArt.OptionsColumn.ReadOnly = true;
            colItem.OwnerBand = bandItem;
            colItem.OptionsColumn.ReadOnly = true;
            //***************************************************************************************         
            
            decimal process = 86;

            String exTotalQty = "";
            String exTotalNeed = "";

            foreach (colHeader node in Listheader)
            {
                BandedGridColumn colcolor = view.Columns.AddVisible("PO:"+node.PO) as BandedGridColumn;
                colcolor.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                colcolor.OwnerBand = bandColor;

                BandedGridColumn colQty = view.Columns.AddVisible("Qty-PO:" + node.PO,"PO:"+node.PO) as BandedGridColumn;
                colQty.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                colQty.DisplayFormat.FormatString = "n0";
                colQty.OptionsColumn.ReadOnly = true;
                colQty.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                colQty.OwnerBand = bandQty;
            }

            //foreach (DataColumn dc in dtbMADetails.Columns)
            //{
            //    if (dc.ColumnName.StartsWith("Color"))
            //    {
            //        BandedGridColumn col = view.Columns.AddVisible(dc.ColumnName) as BandedGridColumn;
            //        col.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            //        col.OwnerBand = bandColor;
            //    }
            //    else if (dc.ColumnName.StartsWith("Quantity"))
            //    {
            //        BandedGridColumn col = view.Columns.AddVisible(dc.ColumnName) as BandedGridColumn;
            //        col.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            //        col.DisplayFormat.FormatString = "n0";
            //        col.OptionsColumn.ReadOnly = true;
            //        col.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            //        col.OwnerBand = bandQty;
            //        exTotalQty += "isnull([" + col.FieldName + "],0)+";

            //        BandedGridColumn colNeed = view.Columns.AddVisible(dc.ColumnName.Replace("Quantity", "Need")) as BandedGridColumn;
            //        colNeed.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            //        colNeed.DisplayFormat.FormatString = "n4";
            //        colNeed.OptionsColumn.AllowEdit = false;
            //        colNeed.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
            //        colNeed.UnboundExpression = "IsNull(IsNull([FinalCons],IsNull([ActCons],IsNull([CusCons],IsNull([EstCons],1)))),1) *[" + dc.ColumnName + "]*IsNull([Wastage],1)";
            //        colNeed.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            //        colNeed.OwnerBand = bandNeed;
            //        exTotalNeed += "isnull(" + colNeed.UnboundExpression + ",0)+";
            //    }
            //    else if (dc.ColumnName.StartsWith("Price"))
            //    {
            //        BandedGridColumn col = view.Columns.AddVisible(dc.ColumnName) as BandedGridColumn;
            //        col.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            //        col.DisplayFormat.FormatString = "c5";
            //        col.OptionsColumn.ReadOnly = true;
            //        col.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            //        col.OwnerBand = bandPrice;
            //    }
                System.Threading.Thread.Sleep(10);
                process += 5 / Convert.ToDecimal(dtbMADetails.Columns.Count);
                progressBarReport.EditValue = Convert.ToInt32(process);
           // }
            if (exTotalQty.Length > 0)
            {
                exTotalQty = exTotalQty.Remove(exTotalQty.Length - 1);
                exTotalNeed = exTotalNeed.Remove(exTotalNeed.Length - 1);
            }
            BandedGridColumn colTotalQty = view.Columns.AddVisible("TotalQty") as BandedGridColumn;
            colTotalQty.OwnerBand = bandQty;
            colTotalQty.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            colTotalQty.DisplayFormat.FormatString = "n0";
            colTotalQty.OptionsColumn.AllowEdit = false;
            colTotalQty.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
            colTotalQty.UnboundExpression = exTotalQty;
            colTotalQty.AppearanceCell.BackColor = Color.Yellow;
            colTotalQty.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

            BandedGridColumn colTotalNeed = view.Columns.AddVisible("TotalNeed") as BandedGridColumn;
            colTotalNeed.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            colTotalNeed.DisplayFormat.FormatString = "n4";
            colTotalNeed.OptionsColumn.AllowEdit = false;
            colTotalNeed.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
            colTotalNeed.UnboundExpression = exTotalNeed;
            colTotalNeed.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            colTotalNeed.OwnerBand = bandNeed;
            colTotalNeed.AppearanceCell.BackColor = Color.Yellow;

            String amountEx = null;
            //DataTable dtColDistinct = cls.GetDataTable("exec New_TrimlistColDistinct " + id);
            //foreach (DataRow drColDistinct in dtColDistinct.Rows)
            //{
            //    foreach (BandedGridColumn bgc in view.Columns)
            //    {
            //        if (bgc.FieldName.StartsWith("Need") || bgc.FieldName.StartsWith("Price"))
            //        {
            //            if (bgc.FieldName.EndsWith(drColDistinct["ColHeader"].ToString()))
            //            {
            //                amountEx += "isnull([" + bgc.FieldName + "],0) * ";
            //            }
            //        }
            //    }
            //    if (amountEx != null)
            //    {
            //        amountEx = amountEx.Remove(amountEx.Length - 2);
            //        if (dtColDistinct.Rows.Count > 1)
            //        {
            //            amountEx += " + ";
            //        }
            //    }
            //}

            //if (dtColDistinct.Rows.Count > 1)
            //{
            //    amountEx = amountEx.Remove(amountEx.Length - 2);
            //}

            BandedGridColumn colAmount = view.Columns.AddVisible("UnitPrice") as BandedGridColumn;
            colAmount.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            colAmount.DisplayFormat.FormatString = "n4";
            colAmount.OptionsColumn.AllowEdit = false;
            colAmount.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
            colAmount.UnboundExpression = amountEx;
            colAmount.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            colAmount.OwnerBand = bandPrice;
            colAmount.AppearanceCell.BackColor = Color.Yellow;

            BandedGridColumn colContent = view.Columns.AddVisible("Content") as BandedGridColumn;
            BandedGridColumn colWL = view.Columns.AddVisible("Width", "Width/Length") as BandedGridColumn;
            BandedGridColumn colSize = view.Columns.AddVisible("Size") as BandedGridColumn;
            BandedGridColumn colSupplier = view.Columns.AddVisible("MASUPID") as BandedGridColumn;

            colContent.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            colWL.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            colSize.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            colSupplier.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

            colContent.OwnerBand = bandItemDetails;
            colContent.OptionsColumn.ReadOnly = true;
            colWL.OwnerBand = bandItemDetails;
            colWL.OptionsColumn.ReadOnly = true;
            colSize.OwnerBand = bandItemDetails;
            colSize.OptionsColumn.ReadOnly = true;
            colSupplier.OwnerBand = bandItemDetails;
            colSupplier.OptionsColumn.ReadOnly = true;

            BandedGridColumn colUnit = view.Columns.AddVisible("Units") as BandedGridColumn;
            BandedGridColumn colEstCons = view.Columns.AddVisible("EstCons") as BandedGridColumn;
            BandedGridColumn colCusCons = view.Columns.AddVisible("CusCons") as BandedGridColumn;
            BandedGridColumn colActCons = view.Columns.AddVisible("ActCons") as BandedGridColumn;
            BandedGridColumn colBookCons = view.Columns.AddVisible("BookCons") as BandedGridColumn;
            BandedGridColumn colFinalCons = view.Columns.AddVisible("FinalCons") as BandedGridColumn;
            BandedGridColumn colWastage = view.Columns.AddVisible("Wastage") as BandedGridColumn;

            colUnit.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            colEstCons.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            colCusCons.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            colBookCons.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            colFinalCons.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            colWastage.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

            colUnit.OwnerBand = bandCons;
            colUnit.OptionsColumn.ReadOnly = true;
            colEstCons.OwnerBand = bandCons;
            colEstCons.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            colEstCons.DisplayFormat.FormatString = "n5";
            colCusCons.OwnerBand = bandCons;
            colCusCons.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;


            colActCons.OwnerBand = bandCons;
            colActCons.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            colActCons.DisplayFormat.FormatString = "n5";
            colActCons.OwnerBand = bandCons;

            colCusCons.DisplayFormat.FormatString = "n5";
            colBookCons.OwnerBand = bandCons;
            colBookCons.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            colBookCons.DisplayFormat.FormatString = "n5";
            colFinalCons.OwnerBand = bandCons;
            colFinalCons.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            colFinalCons.DisplayFormat.FormatString = "n5";
            colWastage.OwnerBand = bandCons;
            colWastage.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            colWastage.DisplayFormat.FormatString = "n5";
            progressBarReport.EditValue = 95;
            System.Threading.Thread.Sleep(10);

            BandedGridColumn colRemark = view.Columns.AddVisible("Remarks") as BandedGridColumn;
            colRemark.ColumnEdit = persistentRepository1.Items["repositoryItemMemoExEditUsing"];
            colRemark.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            colRemark.OwnerBand = bandRemark;

            //if (flagAddClick == false)
            //{
            //    loadLayout();
            //}

            #endregion

            #region setDataSource

            gridControl_Material.DataSource = dtbMADetails;

            #endregion

            System.Threading.Thread.Sleep(10);
            progressBarReport.EditValue = 99;
            progressBarReport.Visible = false;   
        }       
    }
}
